package com.ddw.wotcg.server.utils;

import com.google.gwt.regexp.shared.RegExp;

/**
 * Represents a class for field validation
 * 
 * @author fungus1487
 * 
 */
public abstract class FieldValidation {
	
	private static RegExp EMAIL_VALID = RegExp
			.compile("^([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)$");
	private static RegExp PASSWORD_VALID = RegExp
			.compile("((?=.*\\d)(?=.*[a-z]).{6,20})");
	
	/**
	 * Checks if an input email address is in the correct format
	 * 
	 * @param email
	 *            the email address to check is in the correct format
	 * @return a flag indicating the email is valid or not
	 */
	public static boolean validEmail(String email) {
		// Cannot have a null or empty email
		if (email == null || email.length() == 0) {
			return false;
		}
		
		return EMAIL_VALID.test(email);
	}
	
	/**
	 * Checks if an input password is in the correct format
	 * 
	 * @param password
	 *            the password to check is in the correct format
	 * @return a flag indicating the password is valid or not
	 */
	public static boolean validPassword(String password) {
		// Cannot have a null or empty password
		if (password == null || password.length() == 0) {
			return false;
		}
		
		// Check password contains at least 1 letter and number
		if (PASSWORD_VALID.test(password) == false) {
			return false;
		}
		
		return true;
	}
	
	public static boolean validUsername(String username) {
		// Cannot have a null or empty username
		if (username == null || username.length() == 0) {
			return false;
		}
		return true;
	}
	
}
